package co.yixiang.yshop.module.product.service.storeproductstockwarning;

import cn.hutool.core.util.NumberUtil;
import co.yixiang.yshop.module.message.enums.WechatTempateEnum;
import co.yixiang.yshop.module.message.mq.message.WeixinNoticeMessage;
import co.yixiang.yshop.module.message.mq.producer.WeixinNoticeProducer;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductattrvalue.StoreProductAttrValueDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproductattrvalue.StoreProductAttrValueMapper;
import co.yixiang.yshop.module.system.api.dict.DictDataApi;
import co.yixiang.yshop.module.system.api.dict.dto.DictDataRespDTO;
import co.yixiang.yshop.module.system.enums.MallConfigEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import co.yixiang.yshop.module.product.controller.admin.storeproductstockwarning.vo.*;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductstockwarning.StoreProductStockWarningDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.product.convert.storeproductstockwarning.StoreProductStockWarningConvert;
import co.yixiang.yshop.module.product.dal.mysql.storeproductstockwarning.StoreProductStockWarningMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.*;

/**
 * 商品库存预警 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class StoreProductStockWarningServiceImpl implements StoreProductStockWarningService {

    @Resource
    private StoreProductStockWarningMapper storeProductStockWarningMapper;
    @Resource
    private StoreProductAttrValueMapper storeProductAttrValueMapper;
    @Resource
    private DictDataApi dictDataApi;
    @Resource
    private WeixinNoticeProducer weixinNoticeProducer;

    @Override
    public void checkStockWarning(Long productId, String unique) {
        DictDataRespDTO dictDataRespDTO = dictDataApi.parseDictData("mall_config_dict_type",
                MallConfigEnum.STOCK_WARNING.getCode());
        Integer wainingStock = Integer.valueOf(dictDataRespDTO.getValue());
        StoreProductAttrValueDO storeProductAttrValueDO = storeProductAttrValueMapper.selectOne(new LambdaQueryWrapper<StoreProductAttrValueDO>()
                .eq(StoreProductAttrValueDO::getProductId,productId)
                .eq(StoreProductAttrValueDO::getUnique,unique));
        StoreProductStockWarningDO storeProductStockWarningDO = storeProductStockWarningMapper
                .selectOne(new LambdaQueryWrapper<StoreProductStockWarningDO>()
                .eq(StoreProductStockWarningDO::getGoodsId,productId).eq(StoreProductStockWarningDO::getGoodsSkuId,
                        storeProductAttrValueDO.getId()));
        //小于商城配置的预警值
        if(NumberUtil.compare(storeProductAttrValueDO.getStock(),wainingStock) < 0){
            if(storeProductStockWarningDO != null ){
                storeProductStockWarningDO.setStock(storeProductAttrValueDO.getStock());
                storeProductStockWarningDO.setStockWarning(wainingStock);
                storeProductStockWarningMapper.updateById(storeProductStockWarningDO);
            }else{
                StoreProductStockWarningCreateReqVO createReqVO = new StoreProductStockWarningCreateReqVO();
                createReqVO.setGoodsId(productId);
                createReqVO.setGoodsSkuId(storeProductAttrValueDO.getId());
                createReqVO.setStockWarning(wainingStock);
                createReqVO.setStock(storeProductAttrValueDO.getStock());
                createReqVO.setGoodsSkuText(storeProductAttrValueDO.getSku());
                createStoreProductStockWarning(createReqVO);
            }
            //消息推送
            //公众号订单通知管理员
            weixinNoticeProducer.sendNoticeMessage( new WeixinNoticeMessage()
                    .setProductTitle("商品ID：" + storeProductAttrValueDO.getProductId() + "规格：" + storeProductAttrValueDO.getSku() + "库存告警")
                    .setTempkey(WechatTempateEnum.PRODUCT_STOCK_WARNING.getValue())
                    .setType(WechatTempateEnum.TEMPLATES.getValue()));
        }else{
            //删除库存预警
            if(storeProductStockWarningDO != null){
                storeProductStockWarningMapper.deleteById(storeProductStockWarningDO.getId());
            }

        }
    }

    @Override
    public Long createStoreProductStockWarning(StoreProductStockWarningCreateReqVO createReqVO) {
        // 插入
        StoreProductStockWarningDO storeProductStockWarning = StoreProductStockWarningConvert.INSTANCE.convert(createReqVO);
        storeProductStockWarningMapper.insert(storeProductStockWarning);
        // 返回
        return storeProductStockWarning.getId();
    }




    @Override
    public PageResult<StoreProductStockWarningRespVO> getStoreProductStockWarningPage(StoreProductStockWarningPageReqVO pageReqVO) {
        return storeProductStockWarningMapper.selectPage(pageReqVO);
    }


}
